﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Configuration.Settings;
using ASPNETVN.PORTAL.Components.UI.DataTypes;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.Helpers;
using System.Collections;
using ASPNETVN.PORTAL.Components.Share;

namespace ASPNETVN.PORTAL.Modules.CMS.MENU
{
    public partial class Main : PortalModuleControl
    {
        public Main()
        {
            ModuleSettingItem itemSetting = new ModuleSettingItem(new StringDataType());
            itemSetting.Value = Guid.Empty.ToString();
            itemSetting.Name = "ModuleID của Module CMS";
            itemSetting.Description = "ModuleID của Module CMS";
            itemSetting.SortOrder = 1;
            this.BaseSettings.Add("CMS.ModuleID", itemSetting);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Hashtable settings = this.Settings;
            Guid moduleID = new Guid(settings["CMS.ModuleID"].ToString());

            // Left Pane.
            LiteralControl literal = new LiteralControl("");
            literal.Text += "<div class='MenuLink'>";
            // OptionView = None
            const ModulePages page = ModulePages.Item;

            string url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, page));
            literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Trang nhất</a>", url);

            url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Item, "optionview=None"));
            literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Mới cập nhật</a>", url);

            // OptionView = TopView
            url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Item, "optionview=TopView"));
            literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Xem nhiều nhất</a>", url);

            // OptionView = TopComment
            url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Item, "optionview=TopComment"));
            literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Bình luận nhiều nhất</a>", url);

            //literal.Text += "</div>";

            // Manager Permission.
            if (PortalSecurity.HasManageAuthorization(moduleID))
            {
                literal.Text += "<div class='MenuLinkGroup'>Quản trị nội dung</div>";
                //literal.Text += "<div class='MenuLink'>";

                // Add item
                url = HttpUrlBuilder.BuildAddEditUrl(moduleID, 0, "AddEditItem");
                
                url = url.Substring(2);
                literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Tạo bài viết</a>", url);

                // Item.
                url = HttpUrlBuilder.BuildAddEditUrl(moduleID, 0, "ItemManager");
                url = url.Substring(2);
                literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Danh sách bài viết</a>", url);

                // Comment.
                url = HttpUrlBuilder.BuildShareUrl(moduleID, 0, "CommentManager");
                url = url.Substring(2);
                literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Góp ý / Phản hồi</a>", url);

                // Category.
                url = HttpUrlBuilder.BuildShareUrl(moduleID, 0, "CategoryManager");
                url = url.Substring(2);
                literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Danh mục</a>", url);

                // Group
                url = HttpUrlBuilder.BuildAddEditUrl(moduleID, 0, "GroupManager");
                url = url.Substring(2);
                literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Nhóm bài viết</a>", url);

                // Setup.
                /*
                url = HttpUrlBuilder.BuildAddEditUrl(moduleID, 0, "Setup", "");
                url = url.Substring(2);
                literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» Cài đặt</a>", url);
                */ 
            }

            IList<Category> allCategories = Category.SelectCollectionAll(moduleID);
            IList<Category> topCategories = allCategories.Where(x => x.ParentID == 0).OrderBy(x => x.SortOrder).ToList();
            foreach (Category topCategory in topCategories)
            {
                url = HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Item, "categoryid={0}", topCategory.ID);
                url = url.Substring(2);
                literal.Text += string.Format("<div class='MenuLinkGroup'><a href='{0}'>{1}</a></div>", url, topCategory.Title);
                //literal.Text += "<div class='MenuLink'>";
                IList<Category> childCategories = allCategories.Where(x => x.ParentID == topCategory.ID).OrderBy(x => x.SortOrder).ToList();

                foreach (Category childCategory in childCategories)
                {
                    url = HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Item, "categoryid={0}", childCategory.ID);
                    url = url.Substring(2);

                    literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» {1}</a>", url, childCategory.Title);
                }
            }
            literal.Text += "</div>";
            ContentPane.Controls.Add(literal);
        }
    }
}